import { Modal, Space, message, Dropdown, Menu } from "antd";
import { DownOutlined, ExclamationCircleOutlined } from "@ant-design/icons";
import { useState, forwardRef, useImperativeHandle } from "react";
import Iconfont from "@/components/IconFont";

import SelectActions from "@/views/components/SelectActions";
import { isEmpty } from "lodash";

const LeftBtns = (props, ref) => {
	const { selectedRows, isSkuType, handleCommand, dataSource, query } = props;

	const isSearch = (fn) => {
		const keyList = Object.keys(query).map(item => item);
		if (isEmpty(query) || !keyList.some(item => query[item] != undefined)) {
			return message.warning('请先筛选商品');
		}
		fn && fn();
	}

	function isCheck(fn) {
		if (selectedRows.length > 0) {
			fn && fn(selectedRows);
		} else {
			message.warning("请勾选商品");
		}
	}
	function ChangeSaleStatusBtn(p) {
		const { text, isSkuType, handleCommand, selectedRows } = p;
		const label = isSkuType ? "sku" : "spu";
		const labelUpperCase = label.toUpperCase();
		return (
			<SelectActions
				name={text}
				onClick={() => {
					isCheck(() => {
						Modal.confirm({
							className: "views-modal-confirm",
							icon: <Iconfont type="iconjingshi" />,
							centered: true,
							title: `确定要将该商品${labelUpperCase}的销售状态更改为${text}么？`,
							okText: "确定",
							cancelText: "取消",
							onOk: () => {
								const data = {
									params: {
										ids: selectedRows.map(e => e.id),
										saleStatus: text === "上架" ? 1 : 0
									},
									isSkuType
								};
								handleCommand(`${label}批量${text}`, data);
							}
						});
					});
				}}
			/>
		);
	}

	return (
		<>
			<Space>
				{isSkuType && <SelectActions name="修改库存" onClick={() => isCheck(() => props.handleCommand("批量修改库存"))} />}
				{isSkuType && (
					<SelectActions name="编辑原始商品编码" onClick={() => isCheck(() => props.handleCommand("批量编辑商品编码"))} />
				)}
				{isSkuType && (
					<SelectActions
						name="上传原始商品编码"
						onClick={() =>
							isCheck(() => {
								Modal.confirm({
									className: "views-modal-confirm",
									icon: <Iconfont type="iconjingshi" />,
									centered: true,
									title: "确定要上传选中的商品编码吗？",
									okText: "确定",
									cancelText: "取消",
									onOk: () => {
										const data = {
											params: {
												ids: selectedRows.map(e => e.id),
												saleStatus: "0"
											},
											isSkuType
										};
										props.handleCommand("上传商品编码", data);
									}
								});
							})
						}
					/>
				)}
				<SelectActions
					name="开启/禁止同步"
					options={[
						{
							label: "开启同步库存",
							action() {
								isCheck(() => props.handleCommand("开启同步库存"));
							}
						},
						{
							label: "禁止同步库存",
							action() {
								isCheck(() => props.handleCommand("禁止同步库存"));
								/* TODO: */
								/* isCheck(() => {
									Modal.confirm({
										className: "views-modal-confirm",
										icon: <Iconfont type="iconjingshi" />,
										centered: true,
										title: "确定要禁止库存同步么？",
										okText: "确定",
										cancelText: "取消",
										onOk: () =>
											isCheck(() => {
												// 0-禁止，1-同步可用库存，2-同步虚拟库存
												const data = {
													params: {
														ids: selectedRows.map(e => e.id),
														isSyncInventory: 0 //是否同步库存：1,开启；0，关闭
													},
													isSkuType
												};

												props.handleCommand("禁止同步库存", data);
											})
									});
								}); */
							}
						}
					]}
				/>

				<SelectActions name="手动同步库存" onClick={() => isCheck(() => props.handleCommand("手动同步库存"))} />

				{!isSkuType && (
					<>
						<ChangeSaleStatusBtn text="上架" isSkuType={isSkuType} handleCommand={handleCommand} selectedRows={selectedRows} />
						<ChangeSaleStatusBtn text="下架" isSkuType={isSkuType} handleCommand={handleCommand} selectedRows={selectedRows} />
					</>
				)}
				<SelectActions
					name="生成系统商品"
					options={[
						{
							label: "已勾选的未绑定商品",
							action() {
								isCheck(() => props.handleCommand("已勾选的未绑定商品", selectedRows, '_', query));
							}
						},
						{
							label: "已筛选的未绑定商品",
							action() {
								isSearch(() => props.handleCommand("已筛选的未绑定商品", dataSource, '_', query));
							}
						}
					]}
				/>
			</Space>
		</>
	);
};
export default forwardRef(LeftBtns);
